#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <sys/time.h>

#include "../LGraph/LGraph.h"
#include "../LGraph/Profile.h"

#define PROFILE_FILE "/wrk/stacc-sna/volodymyr/november/binary/profiles_2011_11.bin"
#define JAN2003 1041382800

using namespace std;

typedef pair<vertex_data_type, vertex_id_type> time_id;

int main()
{

    Users_data *profile = Users_data::read_from_binary_file(PROFILE_FILE);

    vector<set<time_id> > data(7, set<time_id>());
    vertex_id_type id;
    ifstream ids("ccs/all_ids");
    
    while (ids >> id)
    {
	User_profile p = profile->user_profiles[id];
        if (p.min_srv1_month_dt > JAN2003)
            data[0].insert(make_pair(p.min_srv1_month_dt, id));
        if (p.min_srv2_month_dt > JAN2003)
            data[1].insert(make_pair(p.min_srv2_month_dt, id));
        if (p.min_srv4_order_dt > JAN2003)
            data[2].insert(make_pair(p.min_srv4_order_dt, id));
        if (p.min_srv5_order_dt > JAN2003)
            data[3].insert(make_pair(p.min_srv5_order_dt, id));
        if (p.min_srv6_order_dt > JAN2003)
            data[4].insert(make_pair(p.min_srv6_order_dt, id));
        if (p.min_srv7_order_dt > JAN2003)
            data[5].insert(make_pair(p.min_srv7_order_dt, id));
        if (p.min_srv8_dt > JAN2003)
            data[6].insert(make_pair(p.min_srv8_dt, id));
    }
    ids.close();

    const string dir = "actions";
    const string actions[7] = {"action1.log", "action2.log", "action4.log", "action5.log",
                        "action6.log", "action7.log", "action8.log"};
    vector<ofstream*> as;
    for (int i = 0; i < 7; i++ )
    {
        string path = dir + '/' + actions[i];
        ofstream *ofp = new ofstream(path.c_str());
        as.push_back(ofp);
    }

    for (int i = 0; i < 7; i++)
        for (set<time_id>::iterator it = data[i].begin(); it != data[i].end(); it++)
            (*as[i]) << it->second << " " << it->first << endl;

    for (int i = 0; i < 7; i++)
    {
        as[i]->close();
        delete as[i];
    }

    return 0;
}

